﻿#include "GreedySnake.h"

#include <windows.h>

void gotoxy1(HANDLE hOut1, int x, int y)
{
	COORD pos;
	pos.X = x;             //横坐标
	pos.Y = y;            //纵坐标
	SetConsoleCursorPosition(hOut1, pos);
}
HANDLE hOut1 = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量

GreedySnake::GreedySnake(GreedyWall& tempWall, GreedyFood& tempFood) :wall(tempWall), food(tempFood)
{
	pHead = NULL;
	isLoop = false;
}

void GreedySnake::initSnake()
{
	destroyPoint();

	addPoint(5, 3);
	addPoint(5, 4);
	addPoint(5, 5);
}

//1.销毁所有节点；
void GreedySnake::destroyPoint()
{
	Point* pCur = pHead;
	while (pHead != NULL)
	{
		pCur = pHead->next;
		delete pHead;

		pHead = pCur;
	}
}

void GreedySnake::addPoint(int x, int y)
{
	//2.创建新节点；
	Point* newPoint = new Point;
	newPoint->x = x;
	newPoint->y = y;
	newPoint->next = NULL;

	//3.如果原来的头结点，不为空，就改为身子=；
	if (pHead != NULL)
	{
		wall.setWall(pHead->x, pHead->y, '=');

		gotoxy1(hOut1, pHead->y * 2, pHead->x);
		cout << "=";
	}

	newPoint->next = pHead;
	//4.更新头部；
	pHead = newPoint;
	wall.setWall(pHead->x, pHead->y, '@');

	gotoxy1(hOut1, pHead->y * 2, pHead->x);
	cout << "@";
}

//5.删除节点；
void GreedySnake::delPoint()
{
	//6.两个节点以上，才去做删除操作；
	if (pHead == NULL || pHead->next == NULL)
	{
		return;
	}

	Point* pCur = pHead->next;
	Point* pPre = pHead;

	while (pCur->next != NULL)
	{
		pPre = pPre->next;
		pCur = pCur->next;
	}

	//7.删除尾节点；
	wall.setWall(pCur->x, pCur->y, ' ');

	gotoxy1(hOut1, pCur->y * 2, pCur->x);
	cout << " ";
	delete pCur;
	pCur = NULL;
	pPre->next = NULL;
}

bool GreedySnake::move(char key)
{
	int x = pHead->x;
	int y = pHead->y;

	switch (key)
	{
	case UP:
		x--;
		break;
	case DOWN:
		x++;
		break;
	case LEFT:
		y--;
		break;
	case RIGHT:
		y++;
		break;
	default:
		break;
	}

	//11.判断，如果下一步碰到的是尾巴，不应该死亡；
	Point* pCur = pHead->next;
	Point* pPre = pHead;

	while (pCur->next != NULL)
	{
		pPre = pPre->next;
		pCur = pCur->next;
	}

	if (pCur->x == x && pCur->y == y)
	{
		//12.碰到尾巴，循环；
		isLoop = true;
	}
	else
	{
		//8.判断用户到达位置是否成功；
		if (wall.getWall(x, y) == '*' || wall.getWall(x, y) == '=')
		{
			addPoint(x, y);
			delPoint();
			system("cls");
			wall.drawWall();

			cout << "游戏得分：" << getScore() << "分！" << endl;
			cout << "GAME OVER!!!" << endl;
			return false;
		}

	}


	//9.移动成功，分两种：吃到食物，未吃到食物；
	if (wall.getWall(x, y) == '#')
	{
		addPoint(x, y);
		//10.重新设置食物；
		food.setFood();
	}
	else
	{
		addPoint(x, y);
		delPoint();
		if (isLoop == true)
		{
			wall.setWall(x, y, '@');

			gotoxy1(hOut1, y * 2, x);
			cout << "@";
		}
	}

	return true;
}

int GreedySnake::getSleepTime()
{
	int sleepTime = 0;

	int size = countList();
	if (size < 5)
	{
		sleepTime = 300;
	}
	else if (size >= 5 && size <= 8)
	{
		sleepTime = 200;
	}
	else
	{
		sleepTime = 100;
	}

	return sleepTime;
}

int GreedySnake::countList()
{
	int size = 0;
	Point* curPoint = pHead;
	while (curPoint != NULL)
	{
		size++;
		curPoint = curPoint->next;
	}
	return size;
}

int GreedySnake::getScore()
{
	int size = countList();

	int score = (size - 3) * 100;
	return score;
}
